package com.hethongviet.preventing.page;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.hethongviet.preventingmachine.Constants;
import com.hethongviet.preventingmachine.R;
import com.hethongviet.preventingmachine.Utils;
import com.hethongviet.preventingmachine.helps.dialog.DialogMessageBack;

public class PageChangeCameraActivity extends Activity
{
	private String _newName = null;
	private String _newAddress = null;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		Utils.prepareWindow(this);

		setContentView(com.hethongviet.preventer.admin.R.layout.page_change_camera);

		final String oldName = getIntent().getStringExtra(Constants.EXTRA_NAME);
		final String oldAddress = getIntent().getStringExtra(
				Constants.EXTRA_ADDRESS);

		final EditText editTextName = (EditText) findViewById(com.hethongviet.preventer.admin.R.id.change_name_text);
		editTextName.setText(oldName);

		final EditText editTextAddress = (EditText) findViewById(com.hethongviet.preventer.admin.R.id.change_address_text);
		editTextAddress.setText(oldAddress);

		final Button buttonAccept = (Button) findViewById(R.id.change_button_accept);
		final Button buttonCancel = (Button) findViewById(R.id.change_button_cancel);

		buttonAccept.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0)
			{
				_newName = editTextName.getText().toString().trim();
				if (Utils.isStringNull(_newName))
				{
					showDialogWithCatch(R.string.message_error_input_null);
					return;
				}

				for (int index = 0; index < _newName.length(); index++)
				{
					char c = _newName.charAt(index);

					if (!Character.isLetterOrDigit(c) && c != '_' && c != '-'
							&& c != ' ')
					{
						showDialogWithCatch(R.string.message_error_wrong_input_name);
						return;
					}
				}

				if (_newName.length() < Constants.LENGTH_MIN_NAME)
				{
					showDialogWithCatch(R.string.message_error_input_too_short_name);
					return;
				}

				if (_newName.length() > Constants.LENGTH_MAX_NAME)
				{
					showDialogWithCatch(R.string.message_error_input_too_long_name);
					return;
				}

				if (_newName.equals(oldName))
				{
					showDialogWithCatch(R.string.message_error_input_name_same_old);
					return;
				}

				_newAddress = editTextAddress.getText().toString().trim();
				if (Utils.isStringNull(_newAddress))
				{
					showDialogWithCatch(R.string.message_error_input_null);
					return;
				}

				if (_newAddress.length() < Constants.LENGTH_MIN_ADDRESS)
				{
					showDialogWithCatch(R.string.message_error_input_too_short_address);
					return;
				}

				if (_newAddress.length() > Constants.LENGTH_MAX_ADDRESS)
				{
					showDialogWithCatch(R.string.message_error_input_too_long_address);
					return;
				}

				if (_newAddress.equals(oldAddress))
				{
					showDialogWithCatch(R.string.message_error_input_address_same_old);
					return;
				}
				
				for (int index = 0; index < _newAddress.length(); index++)
				{
					char c = _newAddress.charAt(index);

					if (!Character.isLetterOrDigit(c) && c != '_' && c != '-'
							&& c != ' '&& c != ':'&& c != '/'&& c != '.')
					{
						showDialogWithCatch(R.string.message_error_wrong_input_name);
						return;
					}
				}

				/* Save */
				Intent intent = new Intent();
				intent.putExtra(Constants.EXTRA_NAME, _newName);
				intent.putExtra(Constants.EXTRA_ADDRESS, _newAddress);
				intent.putExtra(Constants.EXTRA_IDDB, getIntent().getLongExtra(Constants.EXTRA_IDDB, -1));
				setResult(RESULT_OK, intent);
				finish();

			}
		});
		buttonCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0)
			{
				setResult(RESULT_CANCELED);
				finish();
			}
		});
	}

	private void showDialogWithCatch(int id)
	{
		try
		{
			showDialog(id);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	@Override
	protected Dialog onCreateDialog(int id)
	{
		if (id == R.string.message_error_change_name_fail)
		{
			return new DialogMessageBack(this, true, id, true);
		}
		else if (id == R.string.message_success_change_name)
		{
			return new DialogMessageBack(this, false, id, true);
		}
		else if (id == R.string.message_error_input_too_long_name)
		{
			return new DialogMessageBack(this, true, String.format(
					getString(id), Constants.LENGTH_MAX_NAME), false);
		}
		else if (id == R.string.message_error_input_too_short_name)
		{
			return new DialogMessageBack(this, true, String.format(
					getString(id), Constants.LENGTH_MIN_NAME), false);
		}
		else if (id == R.string.message_error_input_too_long_address)
		{
			return new DialogMessageBack(this, true, String.format(
					getString(id), Constants.LENGTH_MAX_ADDRESS), false);
		}
		else if (id == R.string.message_error_input_too_short_address)
		{
			return new DialogMessageBack(this, true, String.format(
					getString(id), Constants.LENGTH_MIN_ADDRESS), false);
		}
		else return new DialogMessageBack(this, true, id, false);
	}

}
